﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace PasswordProvider.ViewModel.Helpers
{
    internal enum AssociationLevel
    {
        Machine,
        Effective,
        User,
    };

    internal enum AssociationType
    {
        FileExtension,
        UrlProtocol,
        StartMenuClient,
        MimeType,
    };

    /// <summary>
    /// Defines the COM interface to use for various functions related to the default program for various file and 
    /// protocal types. 
    /// This code was copied from Jeff Moser at MoserWere: http://www.moserware.com/2009_04_01_archive.html
    /// </summary>
    [ComImport]
    [Guid("4e530b0a-e611-4c77-a3ac-9031d022281b")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IApplicationAssociationRegistration
    {
        /// <summary>
        /// returns the full path to the application which is associated with the query parameter.
        /// </summary>
        [return: MarshalAs(UnmanagedType.LPWStr)]
        string QueryCurrentDefault(
            [MarshalAs(UnmanagedType.LPWStr)] string query,
            AssociationType queryType,
            AssociationLevel queryLevel);

        [return: MarshalAs(UnmanagedType.Bool)]
        bool QueryAppIsDefault(
            [MarshalAs(UnmanagedType.LPWStr)] string query, 
            AssociationType queryType,
            AssociationLevel queryLevel,
            [MarshalAs(UnmanagedType.LPWStr)] 
            string appRegistryName);

        [return: MarshalAs(UnmanagedType.Bool)]
        bool QueryAppIsDefaultAll(
            AssociationLevel queryLevel, 
            [MarshalAs(UnmanagedType.LPWStr)] 
            string appRegistryName);

        void SetAppAsDefault([MarshalAs(UnmanagedType.LPWStr)] 
            string appRegistryName, 
            [MarshalAs(UnmanagedType.LPWStr)] string set, 
            AssociationType setType); 
        
        void SetAppAsDefaultAll([MarshalAs(UnmanagedType.LPWStr)] 
            string appRegistryName);

        void ClearUserAssociations();
    }

    [ComImport]
    [Guid("591209c7-767b-42b2-9fba-44ee4615f2c7")]
    internal class ApplicationAssociationRegistration
    {
        // coclass is implemented by the runtime callable wrapper
    }

}
